#!/bin/bash

# Copyright 2014 Intel Corporation, All Rights Reserved.

# Licensed under the Apache License, Version 2.0 (the"License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#  http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.


set -o xtrace
rm -rf /opt/stack/data/vsm/*

password=`cat /etc/vsmdeploy/deployrc | grep ROOT | awk -F "=" '{print $2}'`
host_list=`mysql -uroot -p$password -Ns -e "use vsm; select host from init_nodes;"`

#------------------------------------------------------------
# Sync code
#------------------------------------------------------------
old_dir=`pwd`
cd $old_dir
fcon=`cat *.patch  | grep "^diff" | grep py | grep vsm | awk '{print $4}' | sort -u`
tdir="/usr/lib/python2.6/site-packages/"
for n in $fcon; do
    file=${n#*vsm}
    fnew="${old_dir}/source/vsm/$file"
    fold="${tdir}/${file}"
    echo $file $fnew $fold
    if [[ ! -e $fnew ]]; then
        continue
    fi

    for hn in $host_list; do
        scp -pr $fnew $hn:$fold
    done
    cp -rf $fnew $fold
done

cd $old_dir

fcon=`git diff  | grep "^diff" | grep py | grep vsm | awk '{print $4}' | sort -u`
tdir="/usr/lib/python2.6/site-packages/"
for n in $fcon; do
    file=${n#*vsm}
    fnew="${old_dir}/source/vsm/$file"
    fold="${tdir}/${file}"
    echo $file $fnew $fold
    if [[ ! -e $fnew ]]; then
        continue
    fi

    for hn in $host_list; do
        scp -pr $fnew $hn:$fold
    done
    cp -rf $fnew $fold
done

set +o xtrace
